#include "../app_status_bar_conf.h"
#include "../inc/app_status_bar_type.h"

#if STATUS_BAR_USE_NOTIFICATION && STATUS_BAR_APP_USE

LV_IMG_DECLARE(app_status_bar_img_clear);

LV_FONT_DECLARE(app_status_bar_font16_reg);

static uint8_t loaded = 0;
static uint16_t info_cnt = 0;

extern app_status_bar_scr_objs_t *app_status_bar_get_objs();

static void obj_set_opa(lv_obj_t *obj, lv_coord_t opa)
{
    lv_obj_set_style_bg_opa(obj, opa, 0);
}

static void img_set_opa(lv_obj_t *img, lv_coord_t opa)
{
    lv_obj_set_style_img_opa(img, opa, 0);
}

static void label_set_opa(lv_obj_t *label, lv_coord_t opa)
{
    lv_obj_set_style_text_opa(label, opa, 0);
}

static void anim_end_del_cb(lv_anim_t *a)
{
    lv_obj_del(a->var);
    app_status_bar_get_objs()->info_clear = NULL;
}

void app_status_bar_expand_msg_cb()
{
    if (loaded)
        return;

    loaded = 1;
    app_status_bar_scr_objs_t *scr_objs = app_status_bar_get_objs();

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_values(&a, 1, 255);
    lv_anim_set_time(&a, 200);

    if (info_cnt)
    {
        scr_objs->info_clear = lv_obj_create(lv_layer_sys());
        lv_obj_clear_flag(scr_objs->info_clear, LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_set_size(scr_objs->info_clear, STATUS_BAR_NOTIFICATIONS_CLEAR_SIZE, STATUS_BAR_NOTIFICATIONS_CLEAR_SIZE);
        lv_obj_set_style_radius(scr_objs->info_clear, LV_RADIUS_CIRCLE, 0);
        lv_obj_set_style_bg_color(scr_objs->info_clear, lv_color_hex(0x7B7B7B), 0);
        lv_obj_set_style_border_width(scr_objs->info_clear, 0, 0);

        lv_obj_align(scr_objs->info_clear, LV_ALIGN_BOTTOM_MID, 0, STATUS_BAR_NOTIFICATIONS_CLEAR_Y_OFFSET);

        lv_obj_t *img = lv_img_create(scr_objs->info_clear);
        lv_img_set_src(img, &app_status_bar_img_noti_clear);
        lv_obj_center(img);

        lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)img_set_opa);
        lv_anim_set_var(&a, img);
        lv_anim_start(&a);

        lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)obj_set_opa);
    }
    else
    {
        scr_objs->info_clear = lv_label_create(scr_objs->notification);
        lv_label_set_text_static(scr_objs->info_clear, "暂无消息通知");
        lv_obj_set_style_text_font(scr_objs->info_clear, &app_status_bar_font_noti, 0);
        lv_obj_set_style_pad_top(scr_objs->info_clear, LV_VER_RES / 2 - STATUS_BAR_HEIGHT, 0);
        lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)label_set_opa);
    }
    lv_anim_set_var(&a, scr_objs->info_clear);
    lv_anim_start(&a);
}

void app_status_bar_hid_msg_end_cb()
{
}

void app_status_bar_hid_msg_start_cb()
{
    if (loaded == 0)
        return;

    loaded = 0;

    app_status_bar_scr_objs_t *scr_objs = app_status_bar_get_objs();
    if (scr_objs->info_clear == NULL)
        return;

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_values(&a, 255, 0);
    lv_anim_set_time(&a, 200);

    if (!lv_obj_check_type(scr_objs->info_clear, &lv_label_class))
    {
        lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)img_set_opa);
        lv_anim_set_var(&a, lv_obj_get_child(scr_objs->info_clear, 0));
        lv_anim_start(&a);

        lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)obj_set_opa);
    }
    else
    {
        lv_anim_set_time(&a, 200);
        lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)label_set_opa);
    }

    lv_anim_set_deleted_cb(&a, anim_end_del_cb);

    lv_anim_set_var(&a, scr_objs->info_clear);
    lv_anim_start(&a);
}

#endif